package ac.biu.nlp.nlp.curator.client.rtesum;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.thrift.TException;
import org.w3c.dom.Document;

import ac.biu.nlp.nlp.curator.client.CuratorClientException;

import edu.illinois.cs.cogcomp.thrift.base.AnnotationFailedException;
import edu.illinois.cs.cogcomp.thrift.base.ServiceUnavailableException;
import eu.excitementproject.eop.common.utilities.datasets.rtesum.Rte6mainIOException;

/**
 * 
 * @author Asher Stern
 * @since Mar 24, 2013
 *
 */
public class RteSumDatasetSrlMain
{
	public static final String HOST = "localhost";
	public static final int PORT = 9010;

	@SuppressWarnings("serial")
	public static final class RteSrlException extends Exception
	{public RteSrlException(String message){super(message);}}
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		try
		{
			RteSumDatasetSrlMain app = new RteSumDatasetSrlMain();
			app.go(args);
		}
		catch(Exception e)
		{
			e.printStackTrace(System.out);
		}

		
	}
	
	public void go(String[] args) throws ParserConfigurationException, RteSrlException, ServiceUnavailableException, AnnotationFailedException, TException, Rte6mainIOException, TransformerException, IOException, CuratorClientException
	{
		int index=0;
		datasetDirecotryName = args[index++];
		outputXmlFileName = args[index++];
		
		createDocument();
		datasetDir = new File(datasetDirecotryName);
		if (!datasetDir.exists()) throw new RteSrlException(datasetDirecotryName+" does not exist");
		if (!datasetDir.isDirectory()) throw new RteSrlException(datasetDirecotryName+" is not a directory");
		
		RteSumDatasetSrl srl = new RteSumDatasetSrl(xmlDocument,HOST,PORT,datasetDir);
		srl.process();
		
		writeDocumentToFile(xmlDocument, new File(outputXmlFileName));
	}
	
	private void createDocument() throws ParserConfigurationException
	{
		xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
	}

	
	private static void writeDocumentToFile(Document document, File file) throws TransformerException, IOException
	{
		TransformerFactory transfac = TransformerFactory.newInstance();
		Transformer trans = transfac.newTransformer();
		trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		trans.setOutputProperty(OutputKeys.INDENT, "yes");
		trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

		//StreamResult streamResult = new StreamResult(this.output);
		FileOutputStream outputStream = new FileOutputStream(file);
		try
		{
			StreamResult streamResult = new StreamResult(outputStream);
			DOMSource source = new DOMSource(document);
			trans.transform(source, streamResult);
		}
		finally
		{
			outputStream.close();
		}
	}
	
	private String datasetDirecotryName;
	private String outputXmlFileName;
	
	private Document xmlDocument;
	private File datasetDir;

}
